var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        vedio : {},
        fee : '',
        inited : false,
    },
    onLoad:function(options){
        var self = this;
        
        var postdata = {
            op:'jifenxp',

        };
        app.com.http('jifen','POST',postdata,0,false,'','',function(res){
            if (!res.data.errno && res.data) {
                self.setData({
                    params : res.data.data.params,
                    inited : true,
                })
                wx.setNavigationBarTitle({
                    title: res.data.data.params.title ? res.data.data.params.title : '上传小票'
                });
            }else{
                app.com.alert(res.data.message,function(){
                    app.com.backPage(0);
                });
            }

        });
        app.com.setBar(app,self);
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.data.isdown = true;
        this.onLoad();
        this.data.isdown = false;
        wx.stopPullDownRefresh();
    },
    onRemoveImgTap: function(e) {
        var index = e.currentTarget.dataset.index;
        this.setData({
            pic: {}
        });
    },
    uptomaster(tempurl,fn,type){
        wx.showLoading({mask:true,title:'上传中'});

        var url = app.siteInfo.siteroot+'?i='+app.siteInfo.uniacid+'&j=&c=utility&a=file&do=upload&type=image&thumb=0';
        if(type == 'vedio'){
            var url = app.com.murl('myinit',{op:'upvedio'});
        }  
        wx.uploadFile({
            url: url,
            filePath: tempurl,
            name: 'file',
            success: function(res){
                var aaa = JSON.parse( res.data );
                if(aaa.attachment){
                    if( fn ) fn( aaa );
                }else{
                    app.com.alert('上传失败'+aaa.res);
                }
            },
            complete : function(){
                wx.hideLoading();
            },
            fail(rs){
                console.log(rs)
            }
        })
    },
    telfn(e){
        this.setData({
            fee : e.detail
        })
    },
    pubstart(){
        var self = this;
        if(self.data.params.xcxmess && wx.requestSubscribeMessage){
            wx.requestSubscribeMessage({
                tmplIds : self.data.params.xcxmess,
                complete : function(){
                    self.pub();
                }
            })
        }else{
            self.pub();
        }
    },
    pub(){
        var self = this;
        if( self.data.isauth ){
            this.loginauth = this.selectComponent('#loginauth');
            this.loginauth.hideauth();
            return false;
        }

        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success (res) {
                for(var t in res.tempFilePaths){
                    (function(m){

                        self.uptomaster(res.tempFilePaths[m],function(resa){

                            var postdata = {
                                op : 'pubxp',
                                pic : resa.attachment,
                            };
                            app.com.http('jifen','POST',postdata,0,true,'','',function(res){
                                
                                if (!res.data.errno && res.data) {
                                    app.com.alert(res.data.message);
                                }else if(res.data.errno == 2){
                                    app.com.confirm(res.data.message,function(){
                                        var tourl = '/zofui_sales/ucard/buycard';
                                        wx.navigateTo({
                                          url: tourl
                                        });
                                    },false,'取消','成为会员');
                                }else{
                                    app.com.alert(res.data.message);
                                }
                            });

                        });

                    })(t)
                }
            }
        })
    },
    onChangetime(e){
        this.data.toptime = e.detail;
        this.countTotal();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    authed(){
        this.setData({
            isauth : false,
        })
        this.pub();
    }
})